﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;
using GestaoContas.LigacaoBD;


namespace GestaoContas
{
    public partial class PesquisaContas : Form
    {
        //private string _tipoUser;
        //connectionString="Data Source=|DataDirectory|JardimBD.sdf" providerName="System.Data.SqlServerCe.4.0"
        private FicheiroConfiguracao _config;

        public PesquisaContas()
        {
            _config = new FicheiroConfiguracao();
            InitializeComponent();
        }

        public PesquisaContas(string tipoUser)
        {
            //_tipoUser = tipoUser;
            _config = new FicheiroConfiguracao();

            InitializeComponent();

            if (tipoUser == "Funcionario")
            {
                verContaToolStripMenuItem.Enabled = false;
                apagarContaToolStripMenuItem.Enabled = false;
                alterarContaToolStripMenuItem.Enabled = false;
            }

            //PreencheGridView();
        }

        private void PreencheGridView(string role)
        {
            //string connectionString = "Data Source=|DataDirectory|JardimBD.sdf";//providerName=System.Data.SqlServerCe.4.0";
            //string connectionString = "Data Source=C:\\Users\\Marley\\Documents\\Visual Studio 2010\\Projects\\Projeto_Build_0\\Projeto_Build_0\\App_Data\\JardimBD.sdf";
            string connectionString = "Data Source=" + _config.GetConnectionStringBd();
            int roleId = 0;
            string query = @"SELECT Role_Id FROM Roles WHERE Nome = '" + role + "'";

            try
            {
                SqlCeConnection conn = new SqlCeConnection(connectionString);
                conn.Open();
                using (SqlCeCommand cmd = new SqlCeCommand(query, conn))
                {
                    using (SqlCeDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        reader.Read();
                        roleId = int.Parse(reader[0].ToString());
                        reader.Close();
                    }
                }
                conn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            query = @"SELECT Nome as ""Nome"", Morada as ""Morada"", Telefone as ""Telefone"" FROM Pessoa WHERE Role_fk = " + roleId; // WHERE Role_Fk = (SELECT Role_Id FROM Roles WHERE Nome = '" + role +"')";

            try
            {
                SqlCeConnection conn = new SqlCeConnection(connectionString);
                SqlCeCommand cmd = new SqlCeCommand(query, conn);

                SqlCeDataAdapter da = new SqlCeDataAdapter();
                DataSet ds = new DataSet();

                da.SelectCommand = cmd;

                da.Fill(ds);
                dataGridViewPesquisaContas.DataSource = ds.Tables[0];
                dataGridViewPesquisaContas.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        private void comboBoxTipoDeContas_SelectedIndexChanged(object sender, EventArgs e)
        {
            PreencheGridView(comboBoxTipoDeContas.Text);

        }
    }

}